//
//  GoodsDetailViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/11.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "GoodsDetailViewController.h"

@interface GoodsDetailViewController ()<UITableViewDelegate,UITableViewDataSource,LHSelectedNavigationViewToolsDelegate>
{
 NSMutableArray *infoArr;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)GoodsBottomView *bottomView;
@property (nonatomic,strong)LHSelectedNavigationViewTools *tools;
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)UILabel *headLabel;

@end

@implementation GoodsDetailViewController
{
    CGFloat _maxContentOffSet_Y ;
}

- (UILabel *)headLabel{
    if (!_headLabel) {
        _headLabel = [[UILabel alloc]init];
        _headLabel.text = @"上拉，返回详情";
        _headLabel.textAlignment = NSTextAlignmentCenter;
        _headLabel.font = [UIFont systemFontOfSize:13];

    }
    _headLabel.frame = CGRectMake(0, 0, kWidth, 40.f);
    _headLabel.textColor = [UIColor lightGrayColor];

    return _headLabel;
}

- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, _tableView.contentSize.height, kWidth, kHeight)];
        _webView.delegate = self;
        _webView.scrollView.delegate = self;
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
        //添加分段选择器
        [self.webView addSubview:self.tools];

        //开始监听webView滚动的偏移量
        [_webView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    }
    return _webView;
}
- (LHSelectedNavigationViewTools *)tools{
    if (!_tools) {
        NSArray *btnTitle = [NSArray arrayWithObjects:@"图文详情",@"产品参数",@"店铺推荐", nil];
        _tools = [[LHSelectedNavigationViewTools alloc]initLHSelectedNavigationViewToolsWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 50) ButtonTitle:btnTitle];

        _tools.delegate = self;

    }
    return _tools;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth , kHeight - 50)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];

        UILabel *tabFootLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth, 60)];
        tabFootLabel.text = @"继续拖动，查看图文详情";
        tabFootLabel.font = [UIFont systemFontOfSize:13];
        tabFootLabel.textAlignment = NSTextAlignmentCenter;
        _tableView.tableFooterView = tabFootLabel;
        //注册cell
        [_tableView registerNib:[UINib nibWithNibName:@"GoodsImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"GoodsImageTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"GoodsDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"GoodsDetailTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"GoodsContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"GoodsContentTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"QuestionTableViewCell" bundle:nil] forCellReuseIdentifier:@"QuestionTableViewCell"];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self setUpBottomView];
    self.automaticallyAdjustsScrollViewInsets = YES;

    [self.view addSubview:self.webView];
    UILabel *hv = self.headLabel;
    [self.webView addSubview:hv];
    [self.headLabel bringSubviewToFront:self.view];


}

//LHSelectedNavigationViewTools的代理方法
-(void)lhSelectedNavigationViewToolsDelegate:(LHSelectedNavigationViewTools *)tools andFlag:(int)flag{
    switch (flag) {
        case 1000:
          [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
            break;
        case 1001:
            [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://github.com/ccgus/fmdb"]]];
            break;
        case 1002:
            [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.jianshu.com/p/de51868f1035"]]];
            break;
        default:
            break;
    }
}

#pragma mark ---- UITableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row == 0) {
            GoodsImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsImageTableViewCell" forIndexPath:indexPath];
            return cell;
        }else if(indexPath.row == 1){
            GoodsDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsDetailTableViewCell" forIndexPath:indexPath];
            return cell;
        }else if(indexPath.row == 2 ){
            GoodsContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsContentTableViewCell" forIndexPath:indexPath];
            return cell;
        }else if (indexPath.row == 3){
            GoodsContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsContentTableViewCell" forIndexPath:indexPath];
            cell.symptomLabel.hidden = YES;
            return cell;
        }else{
            QuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionTableViewCell" forIndexPath:indexPath];
            return cell;
        }
    }


//cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row == 0 || indexPath.row == 1|| indexPath.row == 3) {
            return 200;
        }else if(indexPath.row == 2){
            return 50;
        }else{
            return 100;
        }

}
//创建底部的三个按钮
- (void)setUpBottomView{
    
    GoodsBottomView *bottomView = [[GoodsBottomView alloc]initWithFrame:CGRectMake(0, kHeight  - 50, kWidth, 50) inView:self.view];
    [self.view addSubview:bottomView];

}

//向上拖动进入图文详情页面
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGFloat offsetY = scrollView.contentOffset.y;
    if([scrollView isKindOfClass:[UITableView class]]) // tableView界面上的滚动
    {
        // 能触发翻页的理想值:tableView整体的高度减去屏幕本身的高度
        CGFloat valueNum = _tableView.contentSize.height - kHeight;
        if ((offsetY - valueNum) > _maxContentOffSet_Y)
        {
            [self goToDetailAnimation]; // 进入图文详情的动画

        }
    }

    else // webView页面上的滚动
    {
        NSLog(@"-----webView-------");
        if(offsetY < 0 && -offsetY > _maxContentOffSet_Y)
        {
            [self backToFirstPageAnimation]; // 返回基本详情界面的动画
        }
    }
}

//进入详情的动画
- (void)goToDetailAnimation{

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        _webView.frame = CGRectMake(0, 0, kWidth, kHeight);
        _tableView.frame = CGRectMake(0, -self.view.bounds.size.height, kWidth, self.view.bounds.size.height);
    } completion:^(BOOL finished) {


    }];
}
//返回第一个界面的动画
- (void)backToFirstPageAnimation{

    [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        _tableView.frame = CGRectMake(0, 0, kWidth, self.view.bounds.size.height);
        _webView.frame = CGRectMake(0, _tableView.contentSize.height, kWidth, kHeight);
    } completion:^(BOOL finished) {

    }];
}

//在KVO的代理方法里，根据偏移量完成提示文本的动画
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if (object == _webView.scrollView && [keyPath isEqualToString:@"contentOffset"]) {
        [self headLabAnimation:[change[@"new"] CGPointValue].y];
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }
}

//提示文本的动画的实现代码：

- (void)headLabAnimation:(CGFloat)offsetY
{
    _headLabel.alpha = -offsetY/60;
    _headLabel.center = CGPointMake(kWidth/2, -offsetY/2.f);
    // 图标翻转，表示已超过临界值，松手就会返回上页
    if(-offsetY>_maxContentOffSet_Y){
        _headLabel.textColor = [UIColor redColor];
        _headLabel.text = @"释放，返回详情";
    }else{
        _headLabel.textColor = [UIColor lightGrayColor];
        _headLabel.text = @"上拉，返回详情";
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
